package com.xuchang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuchang.domain.SysRole;
import com.xuchang.model.R;
import com.xuchang.service.SysRoleService;
import com.xuchang.mapper.SysRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
* @author xuchang
* @description 针对表【sys_role(角色)】的数据库操作Service实现
* @createDate 2025-06-29 22:19:06
*/
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole>
    implements SysRoleService{
    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Override
    public boolean isSuperAdmin(Long userId) {
        // 当用户的角色code 为：ROLE_ADMIN 时，该用户为超级的管理员
        // 用户的id->用户的角色->该角色的Code是否为ROLE_ADMIN
        String adminCode = sysRoleMapper.getUserRoleCode(userId);
        if(!StringUtils.isEmpty(adminCode)&&adminCode.equals("ROLE_ADMIN")){
            return true;
        }
        return false;
    }

    @Override
    public Page<SysRole> getRolesList(Page<SysRole> page, String name) {
        LambdaQueryWrapper<SysRole> sysRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysRoleLambdaQueryWrapper.like(!StringUtils.isEmpty(name),SysRole::getName,name);
        Page<SysRole> list = sysRoleMapper.selectPage(page, sysRoleLambdaQueryWrapper);
        return list;
    }
}




